package com.example.knowman.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.dao.knowCategoryLevels;
import com.example.knowman.dao.knowCategoryLevelsSingle;
import com.example.knowman.dao.knowCategoryWithStaffname;
import com.example.knowman.dao.knowCategoryWithStaffnameAndChild;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.ILog4operateService;
import com.example.knowman.service.IStaffListService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/KnowCategory")
public class KnowCategoryController {
    private final IKnowCategoryService iKnowCategoryService;
    private final IStaffListService iStaffListService;

    private final ILog4operateService iLog4operateService;

    public KnowCategoryController(IKnowCategoryService iKnowCategoryService,IStaffListService iStaffListService,ILog4operateService iLog4operateService) {
        this.iKnowCategoryService = iKnowCategoryService;
        this.iStaffListService = iStaffListService;
        this.iLog4operateService = iLog4operateService;
    }

    //9.1简单举例
    @GetMapping("getEasyClass")
    public ResponseEntity<MExKnowCategory9_1FirstClass> getEasyClass(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                      @RequestParam(required = false) String knowCateName) {

        IPage<knowCategory> res = iKnowCategoryService.getEasyClass(pageNum,pageSize,knowCateName);
        Map<String,String> SL = iStaffListService.getIdAndName();
        IPage<knowCategoryWithStaffname> Reslist = new Page<>(res.getCurrent(),res.getSize());
        Reslist.setTotal(res.getTotal());

        List<knowCategoryWithStaffname> newRecords = new ArrayList<>();
        for(knowCategory item : res.getRecords()){
            knowCategoryWithStaffname temp = new knowCategoryWithStaffname();
//            System.out.println(item.getStaffCode());

            temp.setCreatetime(item.getCreatetime());
            temp.setStaffCode(item.getStaffCode());
            temp.setCateId(item.getCateId());
            temp.setParentId(item.getParentId());
            temp.setKnowCateId(item.getKnowCateId());
            temp.setKnowCateName(item.getKnowCateName());
            temp.setNotes(item.getNotes());

            Integer level = temp.getCateId().length() /2;
            if(level>3) level=3;
            temp.setLevel(level);
            temp.setStaffName(SL.get(item.getStaffCode()));
            newRecords.add(temp);
        }

        Reslist.setRecords(newRecords);


        MExKnowCategory9_1FirstClass resMessage = new MExKnowCategory9_1FirstClass();
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        resMessage.setList(Reslist);
        return ResponseEntity.ok(resMessage);
    }


//    //9.1得到大类
//    @GetMapping("getFirstClass")
//    public ResponseEntity<MExKnowCategory9_1FirstClass> getFirstClass(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
//                                                                      @RequestParam(required = false) String knowCateName) {
//
//        List<knowCategory> res = iKnowCategoryService.getFirstClass(knowCateName);
//        IPage<knowCategoryWithStaffname> page = new Page<>(pageNum,pageSize);
//        page.setTotal(res.size());
//        System.out.println(res);
//        List<knowCategoryWithStaffname> list = new ArrayList<knowCategoryWithStaffname>();
////
//
//        for(knowCategory item : res){
//            knowCategoryWithStaffname temp = new knowCategoryWithStaffname();
////            System.out.println(item.getStaffCode());
//
//            temp.setCreatetime(item.getCreatetime());
//            temp.setStaffCode(item.getStaffCode());
//            temp.setCateId(item.getCateId());
//            temp.setKnowCateId(item.getKnowCateId());
//            temp.setKnowCateName(item.getKnowCateName());
//            temp.setNotes(item.getNotes());
//            System.out.println(temp);
//
//            String SN = iStaffListService.getStaffName(temp.getStaffCode());
////            System.out.println(SN);
//            temp.setStaffName(SN);
//            list.add(temp);
//        }
//
//        page.setRecords(list);
//
//        MExKnowCategory9_1FirstClass resMessage = new MExKnowCategory9_1FirstClass();
//        resMessage.setCode(200);
//        resMessage.setTip("查询成功");
//        resMessage.setList(page);
//        return ResponseEntity.ok(resMessage);
//    }

    //9.1得到所有
    @GetMapping("getAll")
    public ResponseEntity<MExKnowCategory9_1AllClass> getAllClass(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                                      @RequestParam(required = false) String knowCateName) {

        IPage<knowCategory> res = iKnowCategoryService.getFirstClass(pageNum,pageSize,knowCateName);
        IPage<knowCategoryWithStaffnameAndChild> Reslist = new Page<>(pageNum,pageSize);
        Reslist.setTotal(res.getTotal());
//        System.out.println(res);
        List<knowCategoryWithStaffnameAndChild> list = new ArrayList<knowCategoryWithStaffnameAndChild>();//1级
//

        for(knowCategory item : res.getRecords()){
            knowCategoryWithStaffnameAndChild temp = new knowCategoryWithStaffnameAndChild();


            temp.setCreatetime(item.getCreatetime());
            temp.setStaffCode(item.getStaffCode());
            temp.setCateId(item.getCateId());
            temp.setKnowCateId(item.getKnowCateId());
            temp.setKnowCateName(item.getKnowCateName());
            temp.setNotes(item.getNotes());

            //填入子列表的child
            List<knowCategory> child = iKnowCategoryService.getLittleClass(item.getKnowCateId());
            List<knowCategoryWithStaffnameAndChild> childwithname = new ArrayList<knowCategoryWithStaffnameAndChild>();
            for(knowCategory item2:child){
                knowCategoryWithStaffnameAndChild temp2 = new knowCategoryWithStaffnameAndChild();


                temp2.setCreatetime(item2.getCreatetime());
                temp2.setStaffCode(item2.getStaffCode());
                temp2.setCateId(item2.getCateId());
                temp2.setKnowCateId(item2.getKnowCateId());
                temp2.setKnowCateName(item2.getKnowCateName());
                temp2.setNotes(item2.getNotes());
                temp2.setStaffName(iStaffListService.getStaffName(temp2.getStaffCode()));
                List<knowCategory> grandchild = iKnowCategoryService.getLittleClass(item2.getKnowCateId());
                List<knowCategoryWithStaffnameAndChild> grandchildwithname = new ArrayList<knowCategoryWithStaffnameAndChild>();
                for(knowCategory item3:grandchild){
                    knowCategoryWithStaffnameAndChild temp3 = new knowCategoryWithStaffnameAndChild();


                    temp3.setCreatetime(item3.getCreatetime());
                    temp3.setStaffCode(item3.getStaffCode());
                    temp3.setCateId(item3.getCateId());
                    temp3.setKnowCateId(item3.getKnowCateId());
                    temp3.setKnowCateName(item3.getKnowCateName());
                    temp3.setNotes(item3.getNotes());
                    temp3.setStaffName(iStaffListService.getStaffName(temp3.getStaffCode()));
                    grandchildwithname.add(temp3);

                }
                temp2.setChildren(grandchildwithname);
                childwithname.add(temp2);

            }
            //
            temp.setStaffName(iStaffListService.getStaffName(temp.getStaffCode()));
            temp.setChildren(childwithname);
            list.add(temp);
        }


        Reslist.setRecords(list);
////
        MExKnowCategory9_1AllClass resMessage = new MExKnowCategory9_1AllClass();
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        resMessage.setList(Reslist);
        return ResponseEntity.ok(resMessage);

    }


//    //9.1输入knowCateId得到小类
//    @GetMapping("getLittleClass")
//    public ResponseEntity<MExKnowCategory9_1LittleClass> getLittleClass(@RequestParam(required = true) Integer knowCateId) {
//
//        List<knowCategory> res = iKnowCategoryService.getLittleClass(knowCateId);
//
//        List<knowCategoryWithStaffname> list = new ArrayList<knowCategoryWithStaffname>();
////
//
//        for(knowCategory item : res){
//            knowCategoryWithStaffname temp = new knowCategoryWithStaffname();
////            System.out.println(item.getStaffCode());
//
//            temp.setCreatetime(item.getCreatetime());
//            temp.setStaffCode(item.getStaffCode());
//            temp.setCateId(item.getCateId());
//            temp.setKnowCateId(item.getKnowCateId());
//            temp.setKnowCateName(item.getKnowCateName());
//            temp.setNotes(item.getNotes());
//            System.out.println(temp);
//
//            String SN = iStaffListService.getStaffName(temp.getStaffCode());
////            System.out.println(SN);
//            temp.setStaffName(SN);
//            list.add(temp);
//        }
//
//
//
//        MExKnowCategory9_1LittleClass resMessage = new MExKnowCategory9_1LittleClass();
//        resMessage.setCode(200);
//        resMessage.setTip("查询成功");
//        resMessage.setList(list);
//        return ResponseEntity.ok(resMessage);
//    }

    //9.1根据单个cateId执行删除
    @GetMapping("DeleteByKnowCateId")
    public ResponseEntity<Message> deleteByCateId(@RequestParam Integer knowCateId) {

//        List<knowCategory> res = iKnowCategoryService.getLittleClass(knowCateId);
        Message mes = new Message();


        mes = iKnowCategoryService.deleteByKnowCateId(knowCateId);

        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"删除了一个知识类别信息，对应知识类别id:"+knowCateId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }

    //9.1编辑
    @GetMapping("Update")
    public ResponseEntity<Message> update(@RequestParam Integer knowCateId,
                                          @RequestParam(required = false) String knowCateName,
                                          @RequestParam(required = false) String notes) {
        Message res = iKnowCategoryService.update(knowCateId,knowCateName,notes);

        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"更新了一个知识类别信息，对应知识类别id:"+knowCateId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }

    //9.1新增
    @GetMapping("Insert")
    public ResponseEntity<Message> Insert(@RequestParam(required = false) String knowCateName,
                                          @RequestParam(required = false) Integer parentId,
                                          @RequestParam(required = false) String notes,
                                          @RequestParam String staffCode) {


        if(parentId !=null){
            knowCategory find=iKnowCategoryService.selectByknowCateId(parentId);
            if(find == null){
                Message parentIdFail = new Message();
                parentIdFail.setCode(100);
                parentIdFail.setTip("父类"+parentId+" 不存在！");
                return ResponseEntity.badRequest().body(parentIdFail);
            }
        }
        Message res = iKnowCategoryService.Insert(knowCateName,parentId,notes,staffCode);

        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"新增了一个知识类别信息";
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }

    //9.1简单举例
    @GetMapping("getSingleById")
    public ResponseEntity<MExKnowCategory9_1Single> getSingleById(@RequestParam Integer knowCateId) {

        knowCategoryWithStaffname res = iKnowCategoryService.getSingleById(knowCateId);


        MExKnowCategory9_1Single mes = new MExKnowCategory9_1Single();
        mes.setSingle(res);
        mes.setCode(200);
        mes.setTip("查找成功");

        return ResponseEntity.ok(mes);

    }

    //返回级联-全部
    @GetMapping("getAllLevels")
    public ResponseEntity<MessageExKnowCategoryLevels> getAllLevels() {

        List<knowCategoryLevels> list = iKnowCategoryService.getAllLevels();
        MessageExKnowCategoryLevels mes = new MessageExKnowCategoryLevels();
        mes.setCode(200);
        mes.setTip("查找成功");
        mes.setList(list);

        return ResponseEntity.ok(mes);



    }


    //返回级联-单一
    @GetMapping("getSingleLevels")
    public ResponseEntity<MessageExKnowCategoryLevels> getSingleLevels(@RequestParam Integer knowCateId) {

        List<knowCategoryLevels> item = iKnowCategoryService.getSingleLevels(knowCateId);
        MessageExKnowCategoryLevels mes = new MessageExKnowCategoryLevels();
        mes.setCode(200);
        mes.setTip("查找成功");
        mes.setList(item);

        return ResponseEntity.ok(mes);



    }


}
